#pragma once
#include "XtStruct.h"
#include "util.h"
#include "SetFunc.h"
//#include "LFData"




class Spi {

public:

	int a;
	Spi * get_ptr() {
		return this;
	}

};


template<typename T>
class OrderPolicy {
public:
	static int m_request;
	static string m_tag;
	static CAccountDetail m_acct;
	static CAccountDetail m_credit;
	static void order_logic(int type, void* buf) {


		// api shoud check


		short msg_type = type;

		switch (msg_type) {
		case MSG_TYPE_TRADE_ENGINE_LOGIN: {

			//const char *data = reinterpret_cast<const char *>(p);
			//bufferevent_write(g_bev, data, strlen(data) + 1);
			break;
		}

		case 20: {

			LFInputOrderField* p = reinterpret_cast<LFInputOrderField*>  (buf);
			COrdinaryOrder order;
			order.m_dPrice = p->price;
			order.m_nOrderNum = p->volume;
			if (p->exchange == Exchange::EXCH_SSE) {
				strcpy_s(order.m_strExchangeID, "SH");
			}
			else {
			
				strcpy_s(order.m_strExchangeID, "SZ");
			}

			strcpy_s(order.m_strInstrumentID, p->ticker);
			strcpy_s(order.m_strAccountKey, m_acct.m_strAccountKey);
			order.m_eOperationType = XTFW_OPT_BUY;
			strcpy_s(order.m_strRemark, "qmt test");

			string msg = "[ex]" + string(order.m_strExchangeID) + " " +
				"[px]" + to_string(order.m_dPrice) + " " +
				"[vol]" + to_string(order.m_nOrderNum) + " " +
				"[ticker]" + string(order.m_strInstrumentID) + " " +
				"[key]" + string(order.m_strAccountKey) + " " +
				"[key]" + to_string(order.m_eOperationType);



			FileAdapter::GetInstance().flush_to_file("policy.log", msg + "\n");

			xt_order(order , m_request++, m_tag.c_str());
			break;
		}

		case 23: {
			LFOrderActionField* p = reinterpret_cast<LFOrderActionField*>  (buf);
			break;

		}

		}
	}
	void set_config(const char* tag, CAccountDetail acct, CAccountDetail credit) {
		m_tag = tag;
		m_acct = acct;
		m_credit = credit;
		string msg = "[m_tag]" + string(m_tag) + " " +
					"[m_acct] " + string(m_acct.m_strAccountKey);
		FileAdapter::GetInstance().flush_to_file("policy.log", msg + "\n");
	}
};

template<typename T>
int OrderPolicy<T>::m_request = 0;


template<typename T>
string OrderPolicy<T>::m_tag = "";


template<typename T>
CAccountDetail OrderPolicy<T>::m_acct;

template<typename T>
CAccountDetail OrderPolicy<T>::m_credit;
